package helloWorld.infoRetriever;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class AlbumInfoHandler extends DefaultHandler {

	private String name;
	private String largestImage;
	private String albumInfo;
	private String mbId;
	

	private boolean topTagsOpen;
	private boolean nameOpen;
	private boolean summaryOpen;
	private boolean largeImageOpen;
	private boolean mbIdOpen;
		
	public AlbumInfoHandler() {
		super();
	}

	public String getMbId() {
		return mbId;
	}
	public String getName() {
		return name;
	}
	public String getImage() {
		return largestImage;
	}
	public String getAlbumInfo() {
		return albumInfo;
	}

	public String toString() {
		return name + " (" + largestImage + ")" + "\n info: " + albumInfo;
	}
	
	public void startDocument() throws SAXException {
		summaryOpen = false;
		topTagsOpen = false;
		nameOpen = false;
		largeImageOpen = false;
		mbIdOpen = false;

		//should be changed while parsing
		name = "Name unknown";
		largestImage = "Image Not Found";
		albumInfo = "No Extra Info Found";
		mbId = "";
	}
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if(!topTagsOpen && qName.equals("name")) {
			nameOpen = true;
		} else if(qName.equals("toptags")) {
			topTagsOpen = true;
		} else if(qName.equals("summary")){
			summaryOpen = true;
		} else if(qName.equals("image")){
			largeImageOpen = true;
		} else if(qName.equals("mbid")){
			mbIdOpen = true;
		}
	}
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(!topTagsOpen && qName.equals("name")){
			nameOpen = false;
		} else if(qName.equals("toptags")){
			topTagsOpen = false;
		} else if(qName.equals("summary")) {
			summaryOpen = false;
		} else if(qName.equals("image")){
			largeImageOpen = false;
		} else if(qName.equals("mbid")){
			mbIdOpen = false;
		}
	}
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(nameOpen || summaryOpen || largeImageOpen || mbIdOpen){
			String help = "";
			for (int i = start; i < start + length; i++) {
			    switch (ch[i]) {
			    case '\\':
			    	help = help.concat("\\\\");
				break;
			    case '"':
			    	help = help.concat("\\\"");
				break;
			    case '\n':
			    	help = help.concat("\\n");
				break;
			    case '\r':
			    	help = help.concat("\\r");
				break;
			    case '\t':
			    	help = help.concat("\\t");
				break;
			    default:
			    	help = help.concat(Character.toString(ch[i]));
				break;
			    }
			}

			if(nameOpen)
				name = help;
			else if(largeImageOpen)
				largestImage = help;
			else if (mbIdOpen)
				mbId = help;
			else
				albumInfo = help;
		}
	}
	
	public void endDocument() throws SAXException {
		summaryOpen = false;
		topTagsOpen = false;
		nameOpen = false;
		largeImageOpen = false;
		mbIdOpen = false;
	}

}
